// 页面form 与 接口data 数据结构互相转换
// 采购记录（台账）
import {separator, imageDomain} from '@/common/js/resource.js';
import tool from '@/common/js/tools.js';
export default {
	/**
	 * 将接口返回的res.data转换成页面的formData
	 * @param {Object} data 接口返回的data
	 * @return {Object} 页面formData
	 */
	newForm(data) {
		/*
		BeiZhu: "1111"
		CaiGouDate: "2024-04-07"
		GongYingShangID: 1
		GongYingShangMingCheng: "小王粮油店"
		GouHuoPingZhengUrl: ""
		JianYanBaoGaoUrl: ""
		QiTaUrl: ""
		QiYeID: 1
		TaiZhangHeadID: 3
		XuKeZhengUrl: ""
		YanShouRen: "steven"
		YingYeZhiZhaoUrl: ""
		ZhuangTai: 1
		*/
		return {
			id: data.TaiZhangHeadID,
			companyId: data.QiYeID,
			purchaseDate: data.CaiGouDate, // 进货日期
			supplierId: data.GongYingShangID, // 供货商
			supplierName: data.GongYingShangMingCheng, // 供货商
			supplierContracts: data.GongYingShangLianXiRen, // 供货商联系人
			supplierContractsMobile: data.GongYingShangLianXiDianHua, // 供货商联系电话
			accepter: data.YanShouRen, // 验收人
			images1: tool.addImageDomain(data.GouHuoPingZhengUrl), // 购货凭证url
			images2: tool.addImageDomain(data.JianYanBaoGaoUrl), // 产品检验报告url
			images3: tool.addImageDomain(data.XuKeZhengUrl), // 供应商许可证url
			images4: tool.addImageDomain(data.YingYeZhiZhaoUrl), // 营业执照url
			images5: tool.addImageDomain(data.QiTaUrl), // 其他url
			remark: data.BeiZhu, // 备注
			// goodsList: [],
		}
	},
	/**
	 * 将接口返回的res.data转换成页面的dataList
	 * @param {Array} list 接口返回的list
	 * @return {Object} 页面dataList
	 */
	newFormList(list) {
		/*
		CaiGouDate: "2024-04-07"
		GongYingShangMingCheng: "小王粮油店"
		ID: 3
		RecordCount: 3
		*/
		let arr = list.map(data => {
			// let goodsList = [{
			// 	name: `${data.ChanPinMingCheng}(${data.ShuLiang})`
			// }]
			return {
				id: data.ID,
				purchaseDate: data.CaiGouDate, // 进货日期
				supplierName: data.GongYingShangMingCheng, // 供货商
				// goodsList
			}
		})
		return arr;
	},
	/**
	 * 将页面页面的formData转换成接口需要的data
	 * @param {Object} form 页面formData
	 * @return {Object} 传给接口的data
	 */
	newApiData(data) {
		return {
			TaiZhangHeadID: data.id || '',
			QiYeID: data.companyId,
			CaiGouDate: data.purchaseDate, // 进货日期
			GongYingShangID: data.supplierId, // 供货商
			YanShouRen: data.accepter, // 验收人
			GouHuoPingZhengUrl: data.images1.replaceAll(imageDomain, ''), // 购货凭证url
			JianYanBaoGaoUrl: data.images2.replaceAll(imageDomain, ''), // 产品检验报告url
			XuKeZhengUrl: data.images3.replaceAll(imageDomain, ''), // 供应商许可证url
			YingYeZhiZhaoUrl: data.images4.replaceAll(imageDomain, ''), // 营业执照url
			QiTaUrl: data.images5.replaceAll(imageDomain, ''), // 其他url
			BeiZhu: data.remark, // 备注
		}
	},
	// 比较新form和原form是否相同（无变化）
	compare(form1, form2){
		// console.log('form1', form1, 'form2', form2)
		return form1.id                      == form2.id                     
			&& form1.companyId               == form2.companyId              
			&& form1.purchaseDate            == form2.purchaseDate           
			&& form1.supplierId              == form2.supplierId             
			// && form1.supplierName            == form2.supplierName           
			// && form1.supplierContracts       == form2.supplierContracts      
			// && form1.supplierContractsMobile == form2.supplierContractsMobile
			&& form1.accepter                == form2.accepter               
			&& form1.images1                 == form2.images1                
			&& form1.images2                 == form2.images2                
			&& form1.images3                 == form2.images3                
			&& form1.images4                 == form2.images4                
			&& form1.images5                 == form2.images5                
			&& form1.remark                  == form2.remark 
	},
}